﻿using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace Geoserver.Rest.Net.Test
{
    /// <summary>
    ///This is a test class for GeoserverTest and is intended
    ///to contain all GeoserverTest Unit Tests
    ///</summary>
    [TestClass()]
    public class GeoserverTest
    {
        private String _serverIp = "localhost";
        private String _serverPort = "8080";
        private String _testWorkspaceName = "TestWorkspace";
        private String _testStoreName = "TestStore";
        private String _testLayerName = "TestLayer";
        private String _testGroupName = "TestGroup";
        private String _testStoreImage = "C:/Temp/16sed490355.tif";
        private Geoserver _geoserver;

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //

        [TestInitialize()]
        public void GeoserverTestInitialize()
        {
            string serverLocation = _serverIp;
            string serverIp = _serverPort;
            _geoserver = new Geoserver(serverLocation, serverIp);
        }

        [TestCleanup()]
        public void GeoserverTestCleanup()
        {
            //clean up here
        }

        #endregion


        /// <summary>
        ///A test for GetServerWorkspaces
        ///</summary>
        [TestMethod()]
        public void GetServerWorkspacesTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);
            IEnumerable<Workspace> actual = _geoserver.GetWorkspaces();
            Assert.IsNotNull(actual, "Failed to retrieve server workspaces");
            Workspace wksp = actual.SingleOrDefault(ws => ws.Name.Equals(_testWorkspaceName));
            Assert.IsNotNull(wksp, "Failed to retrieve server workspace");
            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }

        /// <summary>
        ///A test for AddServerWorkspace
        ///</summary>
        [TestMethod()]
        public void AddServerWorkspaceTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);
            Workspace wksp = _geoserver.GetWorkspaces().SingleOrDefault(ws => ws.Name.Equals(_testWorkspaceName));
            Assert.IsNotNull(wksp, "Failed to add the desired workspace");
            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }

        /// <summary>
        ///A test for GetDefaultWorkspace
        ///</summary>
        [TestMethod()]
        public void GetDefaultWorkspaceTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName); //add at least one workspace to make sure there is at least one
            Workspace actual;
            actual = _geoserver.GetDefaultWorkspace();
            Assert.IsNotNull(actual, "there is no default workspace");
            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }

        /// <summary>
        ///A test for GetWorkspaceLayers
        ///</summary>
        [TestMethod()]
        public void GetWorkspaceLayersTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);
            _geoserver.AddCoverageStore(_testStoreName, _testStoreImage, _testWorkspaceName);
            _geoserver.AddCoverage(_testLayerName, _testWorkspaceName, _testStoreName, Geoserver.SrsType.Epsg4326);
            IEnumerable<Coverage> layers = _geoserver.GetCoverages(_testWorkspaceName);
            Assert.IsTrue(layers.Count() > 0, "Error running GetWorkspaceLayers test");

            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }

        /// <summary>
        ///A test for GetWorkspaceCoverageStores
        ///</summary>
        [TestMethod()]
        public void GetWorkspaceCoverageStoresTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);

            _geoserver.AddCoverageStore(_testStoreName, _testStoreImage, _testWorkspaceName);
            IEnumerable<CoverageStore> coverageStores = _geoserver.GetCoverageStores(_testWorkspaceName);
            Assert.IsTrue(coverageStores.Count() > 0, "Error running GetWorkspaceCoverageStores test");

            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }

        /// <summary>
        ///A test for GetLayerGroups
        ///</summary>
        [TestMethod()]
        public void GetLayerGroupsTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);

            _geoserver.AddCoverageStore(_testStoreName, _testStoreImage, _testWorkspaceName);
            _geoserver.AddCoverage(_testLayerName, _testWorkspaceName, _testStoreName, Geoserver.SrsType.Epsg4326);
            _geoserver.AddLayerGroup(_testGroupName, new List<string>(){_testLayerName}, _testWorkspaceName);

            IEnumerable<LayerGroup> groups = _geoserver.GetLayerGroups(_testWorkspaceName);
            Assert.IsTrue(groups.Count() > 0, "Error running GetLayerGroups test");

            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }

        /// <summary>
        ///A test for AddLayerGroup
        ///</summary>
        [TestMethod()]
        public void AddLayerGroupTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);

            _geoserver.AddCoverageStore(_testStoreName, _testStoreImage, _testWorkspaceName);
            _geoserver.AddCoverage(_testLayerName, _testWorkspaceName, _testStoreName, Geoserver.SrsType.Epsg4326);
            _geoserver.AddLayerGroup(_testGroupName, new List<string>() { _testLayerName }, _testWorkspaceName);

            IEnumerable<LayerGroup> groups = _geoserver.GetLayerGroups(_testWorkspaceName);
            Assert.IsTrue(groups.Count() > 0, "Error running AddLayerGroup test");

            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }

        /// <summary>
        ///A test for AddCoverageStore
        ///</summary>
        [TestMethod()]
        public void AddCoverageStoreTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);

            _geoserver.AddCoverageStore(_testStoreName, _testStoreImage, _testWorkspaceName);
            IEnumerable<CoverageStore> coverageStores = _geoserver.GetCoverageStores(_testWorkspaceName);
            Assert.IsTrue(coverageStores.Count() > 0, "Error running AddCoverageStores test");

            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }

        /// <summary>
        ///A test for AddCoverageLayer
        ///</summary>
        [TestMethod()]
        public void AddCoverageLayerTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);
            _geoserver.AddCoverageStore(_testStoreName, _testStoreImage, _testWorkspaceName);
            _geoserver.AddCoverage(_testLayerName, _testWorkspaceName, _testStoreName, Geoserver.SrsType.Epsg4326);
            IEnumerable<Coverage> layers = _geoserver.GetCoverages(_testWorkspaceName);
            Assert.IsTrue(layers.Count() > 0, "Error running AddCoverageLayer test");

            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }

        /// <summary>
        ///A test for DeleteServerWorkspace
        ///</summary>
        [TestMethod()]
        public void DeleteServerWorkspaceTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);
            Workspace wksp = _geoserver.GetWorkspaces().SingleOrDefault(ws => ws.Name.Equals(_testWorkspaceName));
            if (wksp != null)
            {
                _geoserver.DeleteWorkspace(_testWorkspaceName);
                wksp = _geoserver.GetWorkspaces().SingleOrDefault(ws => ws.Name.Equals(_testWorkspaceName));
                Assert.IsNull(wksp, "Could not delete the server workspace");
            }
            else
            {
                Assert.Inconclusive("could not create a workspace in order to test the desired DeleteServerWorkspace method");
            }
        }

        /// <summary>
        ///A test for DeleteCoverageStore
        ///</summary>
        [TestMethod()]
        public void DeleteCoverageStoreTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);

            _geoserver.AddCoverageStore(_testStoreName, _testStoreImage, _testWorkspaceName);
            IEnumerable<CoverageStore> coverageStores = _geoserver.GetCoverageStores(_testWorkspaceName);
            Assert.IsTrue(coverageStores.Count() > 0, "Error running AddCoverageStores test");

            _geoserver.DeleteCoverageStore(_testWorkspaceName, _testStoreName);

            coverageStores = _geoserver.GetCoverageStores(_testWorkspaceName);
            Assert.IsTrue(coverageStores.Count() == 0, "Error running AddCoverageStores test");

            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }

        /// <summary>
        ///A test for DeleteCoverageStore
        ///</summary>
        [TestMethod()]
        public void DeleteCoverageTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);
            _geoserver.AddCoverageStore(_testStoreName, _testStoreImage, _testWorkspaceName);
            _geoserver.AddCoverage(_testLayerName, _testWorkspaceName, _testStoreName, Geoserver.SrsType.Epsg4326);

            IEnumerable<Coverage> layers = _geoserver.GetCoverages(_testWorkspaceName);
            Assert.IsTrue(layers.Count() > 0, "Error running AddCoverageLayer test");

            _geoserver.DeleteCoverage(_testWorkspaceName, _testStoreName, _testLayerName);

            layers = _geoserver.GetCoverages(_testWorkspaceName, _testStoreName);
            Assert.IsTrue(layers.Count() == 0, "Error running AddCoverageLayer test");

            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }

        /// <summary>
        ///A test for DeleteLayerGroup
        ///</summary>
        [TestMethod()]
        public void DeleteLayerGroupTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);

            _geoserver.AddCoverageStore(_testStoreName, _testStoreImage, _testWorkspaceName);
            _geoserver.AddCoverage(_testLayerName, _testWorkspaceName, _testStoreName, Geoserver.SrsType.Epsg4326);
            _geoserver.AddLayerGroup(_testGroupName, new List<string>() { _testLayerName }, _testWorkspaceName);

            IEnumerable<LayerGroup> groups = _geoserver.GetLayerGroups(_testWorkspaceName);
            Assert.IsTrue(groups.Count() > 0, "Error running DeleteLayerGroup test");

            _geoserver.DeleteLayerGroup(_testGroupName, _testWorkspaceName);

            groups = _geoserver.GetLayerGroups(_testWorkspaceName);
            Assert.IsTrue(groups.Count() == 0, "Error running DeleteLayerGroup test");

            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }

        /// <summary>
        ///A test for GetLayerGroupCoverages
        ///</summary>
        [TestMethod()]
        public void GetLayerGroupCoveragesTest()
        {
            _geoserver.AddWorkspace(_testWorkspaceName);

            _geoserver.AddCoverageStore(_testStoreName, _testStoreImage, _testWorkspaceName);
            _geoserver.AddCoverage(_testLayerName, _testWorkspaceName, _testStoreName, Geoserver.SrsType.Epsg4326);
            _geoserver.AddLayerGroup(_testGroupName, new List<string>() { _testLayerName }, _testWorkspaceName);

            IEnumerable<LayerGroup> groups = _geoserver.GetLayerGroups(_testWorkspaceName);
            Assert.IsTrue(groups.Count() > 0, "Error running GetLayerGroupCoverages test");

            IEnumerable<Coverage> coverages = _geoserver.GetLayerGroupCoverages(_testGroupName, _testWorkspaceName);
            Assert.IsTrue(coverages.Count() > 0, "Error running GetLayerGroupCoverages test");

            _geoserver.DeleteWorkspace(_testWorkspaceName);
        }
    }
}